<?php
namespace app\models\product;

use app\admin\model\BaseModel;
use app\admin\model\ModelTrait;


class Loan extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */

    use ModelTrait;
    protected $hidden = ['edit_time','create_time'];

    //列表
    public static function pageList ($where) {
        $model = new self;
        $sort = "id desc";
        //设置排序
        switch ($where['sort']) {
            //额度高在前
            case 2:
                $sort = "quota desc";
                break;
        }
        $productSeach = productSeach();
        //设置数据查看条件
        $param = array();
        if($where['quota_type'] != 0) $param['quota'] = $productSeach['quotaType'][$where['quota_type']]['value'];
        if($where['product_search'] != 0) $param['max_date'] = $productSeach['termType'][$where['product_search']]['value'];
        $model = $model->where("is_show",1);
        $model = $model->where($param);
//        $param =
        $model = $model->whereOr($param);
        $model = $model->order($sort);

        $list = $model->select()->each(function ($item) {
            $config = SystemConfigMore(['website_domain']);
            $item['info'] = explode("\r\n",$item['info']);
            $item['image'] = str_replace("\\","/",$config['website_domain'] . $item['image']);
        });
        if($list) $list = $list->toArray();
        return $list;
    }



    public static function recommend($useInfo) {
        $config = SystemConfigMore(['website_domain']);
        $model = new self;
        $model = $model->alias("l")->leftJoin("loan_attr a","a.loan_id=l.id");
        $model = $model->where("is_show",1);
        $model = $model->field("a.income,a.isincome,a.accumulation,a.isaccumulation,a.room,a.vehicle,a.policy,a.isroom,a.isvehicle,a.ispolicy,l.name,l.image,l.info,l.url,l.city");
        
        //判断条件
        
        
        $list =  $model->select()->toArray();
        $data = array();
        foreach ($list as $v) {
            $v['info'] = explode("\r\n",$v['info']);
            $v['image'] = str_replace("\\","/",$config['website_domain'] . $v['image']);
            //是否满足非必选条件中的一个
            $Mandatory = false;
            $wrongMandatory = false;

            if (empty($v['income']) && empty($v['accumulation']) && empty($v['room']) && empty($v['vehicle']) && empty($v['policy'])) {
                $Mandatory[] = true;
                $wrongMandatory = true;
            }

            if ($v['income'] == 0 && $v['accumulation'] == 0 && $v['room'] == 0 && $v['vehicle'] == 0 && $v['policy'] == 0) {
                $Mandatory[] = true;
                $wrongMandatory = true;
            }
            
            //判断月收入的条件
            if ($v['income'] != 0) {
                //月收入是否必选
                if ($v['isincome'] == 0) { 
                    //不是必选
                    if ($useInfo['monthly_income']>$v['income']) {
                        $wrongMandatory = true;
                    }
                }else{
                    //是必选
                    if ($useInfo['monthly_income']>$v['income']) {
                        $Mandatory[] = true;
                    }else{
                        $Mandatory[] = false;
                    }
                }
            }

            //判断公积金是否存在
            if ($v['accumulation'] != 0) {

                if ($v['isaccumulation'] == 0) {
                    if($useInfo['accumulation'] != 0) {
                        $wrongMandatory = true;
                    }
                }else{
                    if($useInfo['accumulation'] != 0) {
                        $Mandatory[] = true;
                    }else{
                        $Mandatory[] = false;
                    }
                }
            }


            //判断是否需要房
            if ($v['room'] != 0) {
                if ($v['isroom'] == 0) {
                    if ($useInfo['room'] != 0) {
                        $wrongMandatory = true;
                    }
                }else{
                    if ($useInfo['room'] != 0) {
                        $Mandatory[] = true;
                    }else{
                        $Mandatory[] = false;
                    }
                }
            }


            //判断是否需车
            if ($v['vehicle'] != 0) {
                if ($v['isvehicle'] == 0) {
                    if($useInfo['license_positive'] != '' && $useInfo['license_side'] != '') {
                        $wrongMandatory = true;
                    }
                }else{
                    if ($useInfo['license_positive'] != '' && $useInfo['license_side'] != '') {
                        $Mandatory[] = true;
                    }else{
                        $Mandatory[] = false;
                    }
                }
            }

            //判断是否需要保单
            if ($v['policy'] != 0) {
                if ($v['ispolicy'] == 0) {
                    if($useInfo['insurance'] != 0) {
                        $wrongMandatory = true;
                    }
                }else{
                    if ($useInfo['insurance'] != 0) {
                        $Mandatory[] = true;
                    }else{
                        $Mandatory[] = false;
                    }
                }
            }
            
            //判断城市用户城市 
            if ($v['city'] == 0) {
                $Mandatory[] = true;
            }else{
                $citys = explode(",",$v['city']);
            }

            if (!in_array(false,$Mandatory) && $wrongMandatory) {
                $data[] = $v;
            }

        }
        
        return $data;

    }
    
}
